﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.ICollegeB;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;

namespace StartsEAS_API.Controllers.CollegeContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class CollegeController : ControllerBase
    {
        private readonly ICollegeBLL _collegeBll;
        public CollegeController(ICollegeBLL collegeBll) {
            _collegeBll = collegeBll;
        }
        /// <summary>
        /// 查询所有学院信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllCollege()
        {
            try
            {
                return _collegeBll.GetAllCollege();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllCollege服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据学院编号查询学院
        /// </summary>
        /// <param name="CollegeId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetCollege(int CollegeId)
        {
            try
            {
                return _collegeBll.GetCollege(CollegeId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetCollege服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 添加学院信息
        /// </summary>
        /// <param name="CollegeName">学院名称</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertCollege(string CollegeName)
        {
            try
            {
                return _collegeBll.InsertCollege(CollegeName);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertCollege服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改学院信息
        /// </summary>
        /// <param name="data">学院信息</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdateCollege(College data)
        {
            try
            {
                return _collegeBll.UpdateCollege(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdateCollege服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
